package com.csylh.converter;

import com.csylh.Form.OrderForm;
import com.csylh.dto.OrderDTO;
import com.csylh.entity.OrderDetail;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * Created by 留歌
 * Date: 2018/3/13 0:33
 * Description:
 */
@Slf4j
public class OrderForm2OrderDTO {

    /**
     * 将orderForm中的信息转化到orderDTO中，主要是废品的ID和对应的数量
     * @param  orderForm
     * @return OrderDTO
     */
    public static OrderDTO convert(OrderForm orderForm){
        OrderDTO orderDTO = new OrderDTO();
        BeanUtils.copyProperties(orderForm,orderDTO);
        Gson gson = new Gson();
        List<OrderDetail> orderDetailList = Lists.newArrayList();
        try{
            orderDetailList = gson.fromJson(orderForm.getItems(),
                    new TypeToken<List<OrderDetail>>() {}.getType());
            orderDTO.setOrderDetailList(orderDetailList);
        }catch (Exception e){
            log.info("【格式转化】格式转化错误 String={}",orderForm.getItems());
        }
        return orderDTO;
    }

}
